import pandas as pd
from sklearn import datasets
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
from joblib import dump, load  # 导入 joblib 的保存和加载函数

iris = datasets.load_iris()

df_iris = pd.DataFrame(iris.data, columns=iris.feature_names)
df_iris['target'] = iris.target

dataX = df_iris.drop(columns=["target"]).values
dataY = df_iris['target'].values

x_train, x_test, y_train, y_test = train_test_split(dataX, dataY, test_size=0.2, random_state=123)

model = LogisticRegression(solver="saga")
model.fit(x_train, y_train)
y_pred = model.predict(x_test)

# 打印模型的准确率、分类报告和混淆矩阵
print("模型准确率：", accuracy_score(y_test, y_pred))
print("分类报告：\n", classification_report(y_test, y_pred))
print("混淆矩阵：\n", confusion_matrix(y_test, y_pred))

dump(model, 'iris_logistic_regression.joblib')  # 保存模型
